<template>
  <div class="home">
    <div class="goods_list">

      <div class="goods_item" v-for="item in list" :key="item.id" @click="toInfo(item.id)">
          <img :src="item.pic" alt="">
          <p>{{item.name}}</p>
          <p>{{item.minPrice}}</p>
          <button @click="add( item )">加入购物车</button>
      </div>
   
    </div>
  </div>
</template>

<script>
import {mapMutations} from "vuex"
export default {
  name: 'HomeView',
  data(){
    return { 
      list:[]
    }
  },
  created(){
    this.getList();
  },
  methods:{
    getList(){
      fetch("https://api.it120.cc/xiaochengxu/shop/goods/list")
      .then(res=>res.json())
      .then(res=>{
          console.log(res)
          this.list= res.data;
      })
    },



    add(item){
      let {id,name,minPrice,pic} = item;
      let obj={id,name,minPrice,pic};
      this.$store.commit("add",obj)
    },


    toInfo(id){
      console.log(id)
      //params
      // this.$router.push({name:"info",params:{id}})
      this.$router.push({name:"info",query:{id}})

    }
    

  }
}
</script>


<style  scoped>
  .goods_list{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    text-align: center;
  }
  .goods_item{
    width: 30vw;
  }
  .goods_item >img{
    width: 300px;
    height: 300px;
  }
</style>